package com.cykj.eurekaribbon.utils.sms;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;


/**
 * @version 1.0
 * @author: zhiMing
 * @date: 2021/9/23 1:00
 * @desc: 短信api
 */
public class SMSTool {

//    public static void main(String[] args){
//        String userName = "dragon_zzm";
//        String password = "Dragon@1128";
//        String telephone = "13375903867";
//        /* 验证码有后台随机生成 */
//        String content = "【传一智慧公交】尊敬的乘客，您的验证码为1314，请于2分钟内正确输入。如非本人操作，请忽略此短信。";
//        String httpUrl = "https://api.smsbao.com/sms";
//
//        StringBuffer httpArg = new StringBuffer();
//        httpArg.append("u=").append(userName).append("&");// 账号
//        httpArg.append("p=").append(md5(password)).append("&");// 密码
//        httpArg.append("m=").append(telephone).append("&");// 发生电话
//        httpArg.append("c=").append(encodeUrlString(content, "UTF-8"));// 发生内容
//
//        /* 进行发送请求 */
//        String result = request(httpUrl, httpArg.toString());
//        System.out.println(result);
//    }


    public static String sendVerificationCode(String telephone){
        Random random = new Random();
        String code = "";
        while (code.length()<4){
            code += random.nextInt(9)+"";
        }
        System.out.println(code);
        String userName = "dragon_zzm";
        String password = "Dragon@1128";
        /* 验证码有后台随机生成 */
        String content = "【传一智慧公交】尊敬的乘客，您的验证码为"+code+"，请于2分钟内正确输入。如非本人操作，请忽略此短信。";
        String httpUrl = "https://api.smsbao.com/sms";

        StringBuffer httpArg = new StringBuffer();
        httpArg.append("u=").append(userName).append("&");// 账号
        httpArg.append("p=").append(md5(password)).append("&");// 密码
        httpArg.append("m=").append(telephone).append("&");// 发生电话
        httpArg.append("c=").append(encodeUrlString(content, "UTF-8"));// 发生内容

        /* 进行发送请求 */
        String result = request(httpUrl, httpArg.toString());
        return result;
    }


    /* 对接对象发送的发送请求方法，返回0，即发送成功 */
    public static String request(String httpUrl, String httpArg) {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "?" + httpArg;

        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = reader.readLine();
            if (strRead != null) {
                sbf.append(strRead);
                while ((strRead = reader.readLine()) != null) {
                    sbf.append("\n");
                    sbf.append(strRead);
                }
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /* 将数据进行md5加密 */
    public static String md5(String plainText) {
        StringBuffer buf = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();
            int i;
            buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return buf.toString();
    }

    /* 将信息进行字符集转码 */
    public static String encodeUrlString(String str, String charset) {
        String retStr = null;
        if (str == null)
            return null;
        try {
            retStr = java.net.URLEncoder.encode(str, charset);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return retStr;
    }
}
